#!/bin/sh
# linuxsamu
# chkconfig: 2345 55 25

REDISPORT={{ sentinel_port }}
BASEPATH={{ redis_base }}
EXEC=${BASEPATH}/bin/redis-sentinel
CLIEXEC=${BASEPATH}/bin/redis-cli
PIDFILE=${BASEPATH}/sentinel.pid
CONF="${BASEPATH}/sentinel.conf"
case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting sentinel $REDISPORT ..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for sentinel to shutdown ..."
                    sleep 1
                done
                echo "sentinel stopped"
        fi
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit
esac

